﻿<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.stp_record_detail_o" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using RTGS.Library.Enum;
using RTGS.Library.DataAccess.Rtgs;
using RTGS.Library.Entities.Rtgs;
using RTGS.Web.support;

using System.Configuration;

namespace RTGS.Web.ajaxhandler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>

public class stp_record_detail_o : BaseHandler
    {

         private stpRecordDetailORepo strRecordDal = new stpRecordDetailORepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        //private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Kemenhub"].ConnectionString);
        
    
    
        private string MethodName = string.Empty;
        private string typeOutgoing = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];
            if (context.Request.Params["typeOutgoing"] != null)
                typeOutgoing = context.Request.Params["typeOutgoing"];

            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                //case ("insert"):
                //    context.Response.Write(Insert(context));
                //    break;
                case ("update"):
                    context.Response.Write(Update(context));
                    break;
                //case ("delete"):
                //    context.Response.Write(Delete(context));
                //    break;
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler

        private string Update(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();

            stpRecordDetailO obj = new stpRecordDetailO();

             //GROUP TRN
            obj.recordDetailOtrTrn = context.Request.Params["trn"];
            obj.recordDetailOtrRelatedtrn = context.Request.Params["relatedTrn"];

             //GROUP AMOUNT
            obj.recordDetailOtrAmount = context.Request.Params["amount"];
            obj.recordDetailOstCurrency = context.Request.Params["currency"];
            obj.recordDetailOtrValuedate = context.Request.Params["bussinesDay"];

             //GROUP DEBIT PARTICIPANT
            obj.recordDetailOstFromaccountnumber = context.Request.Params["debitParticipant"];
            obj.recordDetailOstFromaccountname = context.Request.Params["accountDebitParticipant"];


            //GROUP CREDIT PARTICIPANT
            obj.recordDetailOstToaccountnumber = context.Request.Params["creditParticipant"];
            obj.recordDetailOstToaccountname = context.Request.Params["accountCreditParticipant"];


             //GROUP DEBIT PARTICIPANTS CLIENT
            obj.recordDetailOstOriginatingname = context.Request.Params["debitParticipantsClient"];
            obj.recordDetailOstOriginatingnumber = context.Request.Params["accountDebitParticipantsClient"];


            //GROUP CREDIT PARTICIPANTS CLIENT
            obj.recordDetailOstUltimatebenefname = context.Request.Params["creditParticipantsClient"];
            obj.recordDetailOstUltimatebenefaccount = context.Request.Params["accountParticipantsClient"];

            //GROUP DETAIL
            obj.recordDetailOstMemberinfo = context.Request.Params["remitanceInformation"];
            obj.recordDetailOtrRecordtype = context.Request.Params["detailOfCharges"];
            obj.recordDetailOtrTransactioncode = context.Request.Params["transactionSubTypeCode"];
            obj.recordDetailOstPaymentdetails = context.Request.Params["sender"];
                        

            //GROUP PRIORITY

            obj.recordDetailOrecordSwiftOdatetime = DateTime.Now;
            obj.recordDetailOrecordtime = DateTime.Now;

            obj.recordDetailOtrRelatedtrn = context.Request.Params["Id"];

            if (context.Request.Params["typeOutgoing"].ToString().Trim().Contains("repair"))
            {
                obj.recordDetailOrecordStatus = "RP";
            }

            if (context.Request.Params["typeOutgoing"].ToString().Trim().Contains("drop"))
            {
                obj.recordDetailOrecordStatus = "DO";
            }

            if (context.Request.Params["typeOutgoing"].ToString().Trim().Contains("accept"))
            {
                obj.recordDetailOrecordStatus = "WA";
            }
 
                       

            if (strRecordDal.Update(obj))
            {
                helper.Data = obj;
                helper.Message = "Data has been saved!";
                helper.IsSuccess = true;
            }
            else
            {
                helper.IsSuccess = false;
                helper.Message = strRecordDal.GetErrorMessage();


            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

    
    

        private string GetData(HttpContext context)
        {
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? stpRecordDetailO.Fields.recordDetailOtrRecordtype: context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());


            string parameters = string.Empty;

          

            if (typeOutgoing.Trim().ToLower() == "accept")
                parameters = parameters.Trim().Length > 0 ? parameters + " AND UPPER(RECORD_DETAIL_ORECORD_Status) = 'RP' " : " WHERE UPPER(RECORD_DETAIL_ORECORD_Status) = 'RP'";
            if (typeOutgoing.Trim().ToLower() == "drop")
                parameters = parameters.Trim().Length > 0 ? parameters + " AND UPPER(RECORD_DETAIL_ORECORD_Status) IN ('RF','RP') " : " WHERE UPPER(RECORD_DETAIL_ORECORD_Status) IN ('RF','RP')";
            if (typeOutgoing.Trim().ToLower() == "repair")
                parameters = parameters.Trim().Length > 0 ? parameters + " AND UPPER(RECORD_DETAIL_ORECORD_Status) = 'RF' " : " WHERE UPPER(RECORD_DETAIL_ORECORD_Status) = 'RF'";


            string valuedate = ((context.Request.Params["valuedate"]) == null ? string.Empty : context.Request.QueryString["valuedate"].ToString());
            string trn = ((context.Request.Params["trn"]) == null ? string.Empty : context.Request.QueryString["trn"].ToString());
            string batchref = ((context.Request.Params["batchref"]) == null ? string.Empty : context.Request.QueryString["batchref"].ToString());
            string status = ((context.Request.Params["status"]) == null ? string.Empty : context.Request.QueryString["status"].ToString());
            string refno = ((context.Request.Params["refno"]) == null ? string.Empty : context.Request.QueryString["refno"].ToString());
            string tomember = ((context.Request.Params["tomember"]) == null ? string.Empty : context.Request.QueryString["tomember"].ToString());


            parameters = !string.IsNullOrEmpty(valuedate) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_OTR_ValueDate LIKE '%" + valuedate + "%'" : " WHERE RECORD_DETAIL_OTR_ValueDate LIKE '%" + valuedate + "%'")
                                 : parameters;

            parameters = !string.IsNullOrEmpty(trn) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_OTR_TRN LIKE '%" + trn + "%'" : " WHERE RECORD_DETAIL_OTR_TRN LIKE '%" + trn + "%'")
                                 : parameters;

            parameters = !string.IsNullOrEmpty(batchref) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ORECORD_OH_BatchReference LIKE '%" + batchref + "%'" : " WHERE RECORD_DETAIL_ORECORD_OH_BatchReference LIKE '%" + batchref + "%'")
                                 : parameters;


            parameters = !string.IsNullOrEmpty(status) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ORECORD_Status LIKE '%" + status + "%'" : " WHERE RECORD_DETAIL_ORECORD_Status LIKE '%" + status + "%'")
                                 : parameters;

            parameters = !string.IsNullOrEmpty(refno) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ORECORD_RefNo LIKE '%" + status + "%'" : " WHERE RECORD_DETAIL_ORECORD_RefNo LIKE '%" + refno + "%'")
                                 : parameters;

            parameters = !string.IsNullOrEmpty(tomember) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_OTR_ToMember LIKE '%" + tomember + "%'" : " WHERE RECORD_DETAIL_OTR_ToMember LIKE '%" + tomember + "%'")
                                 : parameters;


            
            
            int totalCount = Convert.ToInt32(strRecordDal.SelectScalar(SQL.Function.Aggregate.Count, stpRecordDetailO.Fields.recordDetailOtrRecordtype, parameters));

            decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
            int totalPages = Convert.ToInt32(dc);

            List<stpRecordDetailO> pagedList = strRecordDal.GetList(pageSize, page, sidx, sord, parameters);

            if (!string.IsNullOrEmpty(strRecordDal.GetErrorMessage()))
            {
                //sysTLog log = new sysTLog();
                //log.logEvent = "GetData";
                //log.logMessage = userDal.GetErrorMessage();
                //log.logSource = "sys_t_user.ashx";
                //log.createdBy = base.UserLoggedIn;
                //log.createdDate = DateTime.Now;
                //logDal.Save(log);
            }

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,

            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
        }

        #endregion

    
    }
}
